package com.gem.talkbase.adapter;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import net.tsz.afinal.FinalBitmap;

import com.gem.talkbase.R;

import com.gem.talkbase.bean.Firend;
import com.gem.talkbase.util.LanguageComparator_CN;
import com.gem.talkbase.widget.AssortPinyinList;

import android.R.integer;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class PinyinAdapter extends BaseExpandableListAdapter {




	private List<String>  list1;
	FinalBitmap fb ;
	Map<String, String> map;
	private AssortPinyinList assort = new AssortPinyinList();
	List<HashMap<String, Object>> strList;
	String string2;
	HashMap<String, Object>    hashMap;
	private View context;
	private Context context2;
	private LayoutInflater inflater;
	List<String> list3;
	private LanguageComparator_CN cnSort = new LanguageComparator_CN();

	public PinyinAdapter(Context context2,View view, List<HashMap<String, Object>> list) {
		super();
		this.context2=context2;
		//this.context = view;
		this.inflater = LayoutInflater.from(view.getContext());
		this.strList = list;
		Iterator<HashMap<String, Object>> iterator=list.iterator();
		List<String> list1=new ArrayList<String>();
		map=new TreeMap<String, String>();
		while (iterator.hasNext()) {
			hashMap=iterator.next();
			String unickString=(String)hashMap.get("unick");
			String uimageString=(String)hashMap.get("uimage");

			map.put(unickString, uimageString);

			list1.add(unickString);		  
		}

		//	long time = System.currentTimeMillis();

		sort(list1);

		//	Toast.makeText(context,String.valueOf(System.currentTimeMillis() - time), 1).show();

	}

	private void sort(List<String> list1) {


		for (String str :list1) {
			assort.getHashList().add(str);
		}
		assort.getHashList().sortKeyComparator(cnSort);
		for(int i=0,length=assort.getHashList().size();i<length;i++)
		{
			Collections.sort((assort.getHashList().getValueListIndex(i)),cnSort);
		}

	}

	public Object getChild(int group, int child) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueIndex(group, child);
	}

	public long getChildId(int group, int child) {
		// TODO Auto-generated method stub
		return child;
	}

	public View getChildView(int group, int child, boolean arg2,View contentView, ViewGroup arg4) {
		// TODO Auto-generated method stub
		fb = FinalBitmap.create(context2);
		if (contentView == null) {
			//适配布局 ，给每个好友适配名字==============================
			contentView = inflater.inflate(R.layout.adapter_chat, null);
		}
		TextView textView = (TextView) contentView.findViewById(R.id.name);
		textView.setText(assort.getHashList().getValueIndex(group, child));
		System.out.println(assort.getHashList().getValueIndex(group, child)+"++++++++++++++++++++++++");
		//textView.setText(strList.get(group).get("unick").toString());

		ImageView imageView=(ImageView)contentView.findViewById(R.id.head);

		//把图片对应设置在对应名字前面======================================
	
		fb.display(imageView,map.get(assort.getHashList().getValueIndex(group, child)));

		return contentView;
	}


	public int getChildrenCount(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group).size();
	}

	public Object getGroup(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return assort.getHashList().size();
	}

	public long getGroupId(int group) {
		// TODO Auto-generated method stub
		return group;
	}

	public View getGroupView(int group, boolean arg1, View contentView,ViewGroup arg3) {
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.list_group_item, null);
			contentView.setClickable(true);
		}
		//设置每排字母导航
		TextView textView = (TextView) contentView.findViewById(R.id.name);
		textView.setText(assort.getFirstChar(assort.getHashList().getValueIndex(group, 0)));


		return contentView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public AssortPinyinList getAssort() {
		return assort;
	}



}
